import {Toast} from "react-vant"

const initialState = {
    data:[],//总数据
    childrenData:JSON.parse(window.localStorage.getItem("childrenData")) || [],//内容数据
    copy_data:[],//拷贝出来的子数据
    shopCar:JSON.parse(window.localStorage.getItem("shopcar")) || [],// 购物车数据
    All_checked:false,//全选状态
    ALL_price:0,// 总价
}

const reducer = (state = initialState, { type, payload }) => {
    let newState = JSON.parse(JSON.stringify(state))

    const set_localStorage = (name,data) =>{
        window.localStorage.setItem(name,JSON.stringify(data))
    }

    // 计算总价函数
    const set_all_price = () => {
        let price = 0
        newState.shopCar.forEach(item=>{
            if (item.checked) {
                price += item.count * item.price
            }
        })
        newState.ALL_price = price
    }

    switch (type) {
        //请求总数据
        case "GET_LIST":
            newState.data = payload
            newState.childrenData = payload[0].children
            newState.copy_data = payload[0].children
            set_localStorage("childrenData", newState.childrenData)
            return newState
        // 设置子数据
        case "SET_CHILDREN":
            newState.childrenData = newState.data[payload].children
            newState.copy_data = newState.data[payload].children
            set_localStorage("childrenData", newState.childrenData)
            return newState
        // 筛选数据
        case "FILTER_DATA":
            newState.childrenData = newState.copy_data.filter(item=>item.type === payload)
            return newState
        // 排序
        case "SORT_DATA":
            newState.childrenData = newState.copy_data.sort((a,b)=>{
                if (payload==="从高到低") {
                    return b.price - a.price
                }else{
                    return a.price - b.price  
                }
            })
            return newState
        // 添加购物车
        case "ADD_SHOPCAR":
            const flag = newState.shopCar.some(item=>item.id === payload)
            if (flag) {
                newState.shopCar.forEach((val,i)=>{
                if (val.id === payload) {
                        val.count++
                    }
                })
            }else{
                newState.copy_data.forEach(item=>{
                    if (item.id === payload) {
                        newState.shopCar.push(item)
                        Toast.success('添加成功')
                        
                    }
                })
            }
            set_localStorage("shopcar",newState.shopCar)
            return newState
        // 收藏
        case "SET_LIKE":
            newState.childrenData.forEach(item=>{
                if (item.id === payload) {
                    item.like = !item.like
                    Toast.success({message:item.like?"收藏成功":"取消收藏"})
                }
            })
            return newState

        // 购物车 ++ 
        case "ADD_SHOPCAR_COUNT":
            newState.shopCar.forEach(item=>{
                if (item.id === payload.id) {
                    item.count ++
                    set_localStorage("shopcar",newState.shopCar)
                }
            })
            set_all_price()
            return newState

        // 购物车 --
        case "DEL_SHOPCAR_COUNT":
            newState.shopCar.forEach(item=>{
                if (item.id === payload.id) {
                    item.count --
                    set_localStorage("shopcar",newState.shopCar)
                }
            })
            set_all_price()
            return newState

        // 删除购物车
        case "DEL_SHOPCAR":
            newState.shopCar.forEach((item,index)=>{
                if (item.id === payload.id) {
                    newState.shopCar.splice(index,1)
                    set_localStorage("shopcar",newState.shopCar)
                }
            })
            set_all_price()
            return newState

        // 单选
        case "SET_SHOPCAR_CHECK":
            newState.shopCar.forEach((item)=>{
                if (item.id === payload.id) {
                    item.checked = ! item.checked
                    set_localStorage("shopcar",newState.shopCar)
                }
            })
            set_all_price()
            newState.All_checked = newState.shopCar.every(item=>item.checked) 
            return newState
        
         // 全选
         case "ALL_SHOPCAR_CHECKED":
            newState.All_checked = payload
            newState.shopCar.forEach((item)=>{
                item.checked = payload
                set_localStorage("shopcar",newState.shopCar)
            })
            set_all_price()
            return newState 
        //全部删除
        case "ALL_DEL_SHOPCAR":
            for (let index =  newState.shopCar.length-1; index > -1; index--) {
                newState.shopCar.forEach((item,i)=>{
                    if (item.checked) {
                        newState.shopCar.splice(i,1)
                    }
                })
            }

            set_all_price()
            return newState 
            
        default:
            return newState
    }
}

export default reducer